package com.ywjt.pinkelephant.home;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stx.xhb.xbanner.XBanner;
import com.ywjt.pinkelephant.R;
import com.ywjt.pinkelephant.base.BaseVPFragment;
import com.ywjt.pinkelephant.home.activity.ActivityAddNewText;
import com.ywjt.pinkelephant.home.activity.ActivityPopuSet;
import com.ywjt.pinkelephant.home.activity.ActivityPromptBroad;
import com.ywjt.pinkelephant.home.activity.ActivityPromptShot;
import com.ywjt.pinkelephant.home.activity.ActivityTool;
import com.ywjt.pinkelephant.home.adapter.HomeAdapter;
import com.ywjt.pinkelephant.widget.MoreBottomDialog;

import java.util.ArrayList;
import java.util.List;


public class HomeIndex extends BaseVPFragment implements View.OnClickListener {

    private static final int REQUEST_CODE = 101;
    private AlertDialog mOpenPermission;
    private static final int REQUEST_CODES = 100;

    private Context context;
    private View view;
    private XBanner banner;
    private List<Integer> banners;
    private HomeAdapter adapter;
    private RecyclerView rcCommon;

    private LinearLayout llAddNewtext,llTool;


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llAddNewtext:
                ActivityAddNewText.actionStart(context);
                break;
            case R.id.llTool:
                ActivityTool.actionStart(context);
                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {

    }

    public static HomeIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
     HomeIndex fl = new HomeIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.home_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        banner=view.findViewById(R.id.banner);
        rcCommon=view.findViewById(R.id.rcCommon);
        llAddNewtext=view.findViewById(R.id.llAddNewtext);
        llTool=view.findViewById(R.id.llTool);
        llAddNewtext.setOnClickListener(this::onClick);
        llTool.setOnClickListener(this::onClick);

        banners=new ArrayList<>();
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banner.setData(R.layout.item_banner1,banners,null);
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object object, View view, int position) {
                RelativeLayout container=(RelativeLayout) view;
                ImageView ivBanner=container.findViewById(R.id.ivBanner);
                Glide.with(context).
                        load(banners.get(position)).into(ivBanner);
            }
        });

        adapter=new HomeAdapter(banners);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
            @Override
            public boolean canScrollVertically() {
                // 直接禁止垂直滑动
                return false;
            }
        };
        rcCommon.setLayoutManager(layoutManager);
        rcCommon.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.llPopuText:
                        //悬浮提词  未完成！！！！！
                        ActivityPopuSet.actionStart(context);

                        break;
                    case R.id.tvPromptShot:
                        checkPermission();
                        break;
                    case R.id.tvPromptBroad:
                        ActivityPromptBroad.actionStart(context);
                        break;
                    case R.id.ivMore:
                        //更多
                        MoreBottomDialog moreBottomDialog=new MoreBottomDialog();
                        moreBottomDialog.setClicklistener(new MoreBottomDialog.ClickListenerInterface() {
                            @Override
                            public void takePhoto() {
                                moreBottomDialog.dismiss();
                            }

                            @Override
                            public void selectFromAlbum() {
                                moreBottomDialog.dismiss();
                            }
                        });
                        moreBottomDialog.show(getChildFragmentManager());
                        break;
                }
            }
        });
    }


    /****权限**/
    private void checkPermission(){
        try{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (PermissionChecker.checkSelfPermission(context, Manifest.permission.CAMERA)!= PermissionChecker.PERMISSION_GRANTED) {
                    // 不相等 请求授权
                    ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA
                    },REQUEST_CODE);
                }else {
                    ActivityPromptShot.actionStart(context);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    ActivityPromptShot.actionStart(context);
                } else {
                    Toast.makeText(context, "您已拒绝申请权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }



}